package com.qianguo.buyer.api;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.baidu.mapapi.map.Text;
import com.eva.androidlibrary.api.CommonApi;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.api.entity.BaseBean;
import com.eva.androidlibrary.api.entity.ProductList;
import com.eva.androidlibrary.api.entity.ShopListBean;
import com.qianguo.buyer.entity.AddShopCar;
import com.qianguo.buyer.entity.BuyerAddress;
import com.qianguo.buyer.entity.BuyerAddressList;
import com.qianguo.buyer.entity.BuyerOrder;
import com.qianguo.buyer.entity.BuyerOrderList;
import com.qianguo.buyer.entity.DeliveryTime;
import com.qianguo.buyer.entity.OrderSN;
import com.qianguo.buyer.entity.PayInfo;
import com.qianguo.buyer.entity.ShopCar;
import com.qianguo.buyer.entity.ShopCarNum;


/**
 * Class description
 *
 * @author GUXIA318
 * @date 2015-11-04 14:09
 */
public class BuyerApi extends CommonApi {

    private static BuyerApi sInstance = null;

    // 公司IP
//    private  String hostUrl = "http://10.59.90.54:8080";

    // 家里IP
//    private  String hostUrl = "http://192.168.0.101:8080";

    public BuyerApi(Context context) {
        super(context);
    }

    /**
     * 初次使用时调用此方法获取实例
     *
     * @return
     */
    public synchronized static BuyerApi getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new BuyerApi(context);
        }
        return sInstance;
    }

    public void getNearByShop(int page, int perpage, String center, int radius, HttpRequestCallback<ShopListBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perpage));
        params.put("center", center);
        params.put("radius", Integer.toString(radius));
        post(ShopListBean.class, hostUrl, "/1.0/search/shop", params, callback);
    }

    /**
     * 获取水果商品列表
     *
     * @param page
     * @param pageSize
     * @param shopId
     * @param categoryId
     * @param orderType
     * @param callback
     */
    public void getShopFruitList(int page, int pageSize, String shopId, int categoryId, int orderType, HttpRequestCallback<ProductList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("shopid", shopId);
        if (categoryId != 0) {
            params.put("categoryid", Integer.toString(categoryId));
        }

        if (orderType != 0) {
            params.put("order_type", Integer.toString(orderType));
        }

        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(pageSize));
        post(ProductList.class, hostUrl, "/1.0/search_product/list", params, callback);
    }

    /**
     * 获取订单列表
     *
     * @param token
     * @param type
     * @param page
     * @param perpage
     */
    public void getOrders(String token, int type, int page, int perpage, HttpRequestCallback<BuyerOrderList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("type", Integer.toString(type));
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perpage));
        post(BuyerOrderList.class, hostUrl, "/1.0/order_buy/list", params, callback);
    }

    /**
     * 添加购物车
     */
    public void addShopCar(String token, String productid, String shopid, int num, int is_leijia, int is_selected, HttpRequestCallback<AddShopCar> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("productid", productid);
        params.put("shopid", shopid);
        params.put("num", Integer.toString(num));
        params.put("is_leijia", Integer.toString(is_leijia));
        params.put("is_selected", Integer.toString(is_selected));
        post(AddShopCar.class, hostUrl, "/1.0/cart_cart/add", params, callback);
    }

    /**
     * 编辑购物车
     */
    public void editShopCar(String token, int cartid, int num, int is_selected, HttpRequestCallback<BaseBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("cartid", Integer.toString(cartid));
        params.put("num", Integer.toString(num));
        params.put("is_selected", Integer.toString(is_selected));
        post(BaseBean.class, hostUrl, "/1.0/cart_cart/edit", params, callback);
    }

    /**
     * 刪除购物车
     */
    public void deleteShopCar(String token, int cartid, HttpRequestCallback<BaseBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("cartid", Integer.toString(cartid));
        post(BaseBean.class, hostUrl, "/1.0/cart_cart/delete", params, callback);
    }

    /**
     * 清空购物车
     */
    public void deleteAllShopCar(String token, String shopid, HttpRequestCallback<BaseBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shopid);
        post(BaseBean.class, hostUrl, "/1.0/cart_cart/deleteall", params, callback);
    }

    /**
     * 获取购物车列表
     */
    public void getShopCarList(String token, String shopid, HttpRequestCallback<ShopCar> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        if (!TextUtils.isEmpty(shopid)) {
            params.put("shopid", shopid);
        }
        post(ShopCar.class, hostUrl, "/1.0/cart_cart/get", params, callback);
    }

    /**
     * 获取购物车商品数量
     */
    public void getShopCarListNum(String token, String shopid, HttpRequestCallback<ShopCarNum> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shopid);
        post(ShopCarNum.class, hostUrl, "/1.0/cart_cart/getnum", params, callback);
    }

    /**
     * 获取买家订单详情
     *
     * @param token
     * @param orderSN
     * @param callback
     */
    public void getOrderDetail(String token, String orderSN, HttpRequestCallback<BuyerOrder> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("ordersn", orderSN);
        post(BuyerOrder.class, hostUrl, "/1.0/order_buy/view", params, callback);
    }

    /**
     * 获取订单确认页的购物车商品列表
     *
     * @param token
     * @param cartid
     * @param callback
     */
    public void getShopCarIndex(String token, String cartid, HttpRequestCallback<ShopCar> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("cartid", cartid);
        post(ShopCar.class, hostUrl, "/1.0/cart_sub/index", params, callback);
    }

    /**
     * 确定下单
     *
     * @param callback
     */
    public void confirmShopCarOrder(String token, String cartid, String realname, String phone, String address, String delivery_time, String remark, HttpRequestCallback<OrderSN> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("cartid", cartid);
        params.put("realname", realname);
        params.put("phone", phone);
        params.put("address", address);
        params.put("delivery_time", delivery_time);
        params.put("remark", remark);
        post(OrderSN.class, hostUrl, "/1.0/cart_sub/order", params, callback);
    }

    /**
     * 获得下单时间参数
     *
     * @param callback
     */
    public void getDeliveryTimeData(HttpRequestCallback<DeliveryTime> callback) {
        post(DeliveryTime.class, hostUrl, "/1.0/cart_config/time", null, callback);
    }

    /**
     * 添加收货地址
     *
     * @param token
     * @param address
     * @param callback
     */
    public void addAddress(String token, BuyerAddress address, HttpRequestCallback<String> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("receiver", address.getReceiver());
        params.put("mobile", address.getMobile());
        params.put("address", address.getAddress());
        params.put("is_default", Integer.toString(address.getIsDefault()));

        if (address.getProvinceId() > 0) {
            params.put("province_id", Integer.toString(address.getProvinceId()));
        }

        if (!TextUtils.isEmpty(address.getProvince())) {
            params.put("province", address.getProvince());
        }

        if (address.getPoiId() > 0) {
            params.put("province_id", Integer.toString(address.getPoiId()));
        }

        if (!TextUtils.isEmpty(address.getPoiName())) {
            params.put("province", address.getPoiName());
        }

        if (!TextUtils.isEmpty(address.getZipcode())) {
            params.put("province", address.getZipcode());
        }

        if (address.getAddType() > 0) {
            params.put("add_type", Integer.toString(address.getAddType()));
        }

        post(String.class, hostUrl, "/1.0/my_address/add", params, callback);
    }

    /**
     * 获取收货地址列表
     * @param token
     * @param callback
     */
    public void getAddressList(String token, HttpRequestCallback<BuyerAddressList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        post(BuyerAddressList.class, hostUrl, "/1.0/my_address/list", params, callback);
    }

    /**
     * 支付订单
     *
     * @param token
     * @param orderSN
     * @param payType
     * @param callback
     */
    public void payOrder(String token, String orderSN, int payType, HttpRequestCallback<PayInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("ordersn", orderSN);
        params.put("pay_type", Integer.toString(payType));
        post(PayInfo.class, hostUrl, "/1.0/pay_order/buy", params, callback);

    }
}
